package TUILayer;
import ControlLayer.*;
import ModelLayer.*;
import java.util.*;

/**
 * Write a description of class LeaseUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeaseUI
{
    // instance variables - replace the example below with your own
    private MainMenuUI mmu;
    private LeaseCtrl leaseCtrl;

    /**
     * Constructor for objects of class LeaseUI
     */
    public LeaseUI()
    {
        // initialise instance variables
        mmu = new MainMenuUI();
        leaseCtrl = new LeaseCtrl();
    }

     public void startMainMenu()
    {
        boolean _exit = false;
        while (!_exit) //! means while exit not is true (that is: false)
        {
            int _choice = writeMainMenu();
            if(_choice == 1)
            {
                createLease();
            }
            else{
                    if(_choice == 2)
                    {
                        getLease(inputLeaseId());
                    }
                    else
                    {
                        if(_choice == 3)
                        {
                            endLease();
                        }
                        else
                        {
                            if(_choice == 4)
                            {
                                listAllLeases();
                            }
                            else
                            {
                                if(_choice == 5)
                                {
                                    mmu.mainMenuStart();
                                    _exit = true;
                                }
                                else
                                {
                                    
                                }
                            }
                        }
                    }
             }
        }
    }
           
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" *** LEASE MENU ***");
            System.out.println(" (1) Create Lease");
            System.out.println(" (2) Show Lease Information");
            System.out.println(" (3) End Lease");
            System.out.println(" (4) List all leases");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    private long inputLeaseId()
    {   
        boolean ok = false;
        long leaseId = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Lease ID:  ");
           try
           {
             leaseId = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return leaseId;
    }
    
    private long inputCustomerId()
    {   
        boolean ok = false;
        long customerId = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Customer ID:  ");
           try
           {
             customerId = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return customerId;
    }
    
    private long inputRentItemId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Rent Item ID:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private String inputStartDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type start date: ");
        String startDate = keyboard.nextLine();
        
        return startDate;
    }
    
    private String inputSpecReturnDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type specified return date: ");
        String specReturnDate = keyboard.nextLine();
        
        return specReturnDate;
    }
    
    private String inputActReturnDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type actual return date: ");
        String actReturnDate = keyboard.nextLine();
        
        return actReturnDate;
    }
    
    private String inputStatus()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type status: ");
        String status = keyboard.nextLine();
        
        return status;
    }
    
    private void getLease(long _id)
    {
        if(leaseCtrl.getLease(_id) != null)
        {
            Lease e = leaseCtrl.getLease(_id);
            System.out.println("ID: " + e.getId());
            System.out.println("Start date: " + e.getStartDate());
            System.out.println("Specified return date: " + e.getSpecReturnDate());
            System.out.println("Customer ID: " + e.getCustomer().getId());
            System.out.println("Rent Item ID: " + e.getRentItem().getId());
            if(e.getActReturnDate() != null)
            {
                System.out.println("Actual return date: " + e.getActReturnDate());
            }
            if(e.getStatus() == true)
            {
                System.out.println("Status: Lease is still in progress");
            }
            else
            {
                System.out.println("Status: Lease has ended");
            }
        }
        else
        {
            System.out.println("Lease with this ID does not exist!");
        }
    }
    
    private void createLease()
    {
        long id = inputLeaseId();
        
        if(leaseCtrl.getLease(id) == null)
        {
            long customerId = inputCustomerId();
            if(leaseCtrl.getCustomer(customerId) != null)
            {
                long rentItemId = inputRentItemId();
                if(leaseCtrl.getRentItem(rentItemId) != null)
                {
                    if(leaseCtrl.getRentItem(rentItemId).getStatus() == true)
                    {
                        Lease l = new Lease();
                        l.setId(id);
                        l.setStartDate(inputStartDate());
                        l.setSpecReturnDate(inputSpecReturnDate());
                        l.setCustomer(leaseCtrl.getCustomer(customerId));
                        l.setRentItem(leaseCtrl.getRentItem(rentItemId));
                        leaseCtrl.getRentItem(rentItemId).setStatusFalse();
            
                        leaseCtrl.createLease(l);
                    }
                    else
                    {
                        System.out.println("This item is currently unavailiable");
                    }
                }
                else
                {
                    System.out.println("Rent Item with this ID does not exist");
                }
            }
            else
            {
                System.out.println("Customer with this ID does not exist");
            }
        }
        
        else
        {
            System.out.println("Lease with this ID already exists");
        }
    }
    
    private void endLease()
    {
        long id = inputLeaseId();
        if(leaseCtrl.getLease(id) != null)
        {
            String actReturnDate = inputActReturnDate();
            leaseCtrl.endLease(id, actReturnDate);
            leaseCtrl.getLease(id).getRentItem().setStatusTrue();
        }
        else
        {
            System.out.println("Lease with this ID does not exist");
        }
    }
    
    private void listAllLeases()
    {
        if (leaseCtrl.listAllLeases().size() == 0 )
        {
            System.out.println("There are no Leases in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<leaseCtrl.listAllLeases().size(); i++)
            {  
                System.out.println (i+1 +")" + "  ID: " + leaseCtrl.listAllLeases().get(i).getId());
            }
        }
    }
}
